/*
 * @Author: chunlai
 * @Date: 2025-02-13 15:17:31
 * @LastEditTime: 2025-08-22 09:59:25
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\views\preinsure\exam\mixs.js
 * @Description:
 */
import { mapGetters } from "vuex";
export const examInsureMixin = {
  data() {
    return {
      jsonName: "",
      configs: {},
      quesList: [],
      eleId: "",
    };
  },
  computed: {
    ...mapGetters({
      preInsConfig: "home/preInsConfig",
      productName: "varietyInfo/productName",
    }),
    askAll() {
      let res = true;
      const len = this.quesList.length;
      for (let i = 0; i < len; i++) {
        const item = this.quesList[i];
        if (item.text && item.value == "") {
          this.eleId = `exam${i}`;
          res = false;
          break;
        }
      }
      return res;
    },
  },
  created() {
    if (this.preInsConfig && this.preInsConfig.jsonName) {
      this.jsonName = this.preInsConfig.jsonName;
    }
    this.initPageConfig();
  },
  methods: {
    initPageConfig() {
      this.$store.commit("updateLoad", true);
      this.$store.dispatch("json/getNoticeJson", this.jsonName).then((res) => {
        this.configs = res;
        let quesArr = [];
        if (this.$getMessage("questionnaire")) {
          quesArr = this.$getMessage("questionnaire");
        } else {
          quesArr = res.queList;
        }
        this.quesList = quesArr;
        this.$store.commit("updateLoad", false);
      });
    },
    comptedName(value) {
      let res = value;
      if (value.indexOf("proName") > -1) {
        res = value.replace(/#.+?#/g, `${this.productName}`);
      }
      return res;
    },
    submit() {
      if (!this.askAll) {
        this.$GlobalFuns.removeToViewEle(this.eleId);
        return this.$toast("请完成问卷里所有题目的答案选择");
      }
      let err = [];
      const len = this.quesList.length;
      for (let i = 0; i < len; i++) {
        const item = this.quesList[i];
        if (item.value) {
          const obj = item.radios.find((e) => e.value == item.value);
          if (obj && obj.error) {
            err.push(`·${obj.error}`);
          }
        }
      }
      if (err.length > 0) {
        let msg =
          "根据您的问卷结果，您暂时无法继续投保，需重新评估。详细原因如下：<br><br>";
        msg += err.join("<br><br>");
        this.$dialog
          .confirm({
            title: `问卷结果`,
            message: `${msg}`,
            messageAlign: "left",
            confirmButtonColor: "#DA251E",
            confirmButtonText: "重新评估",
            cancelButtonText: "取消投保",
            showCancelButton: true,
          })
          .then(() => {})
          .catch(() => {});
        return;
      } else {
        this.goNextStep();
      }
    },
    goNextStep() {
      this.$setMessage("questionnaire", this.quesList);
      // this.$router.push("/form");
    },
  },
};
